﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GUESTSMANAGEMENT.Controller
{
    public class GuestsList
    {

        public static void addListsGuests(gmlistguests obj)
        {
            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {
                context.AddTogmlistguests(obj);
                context.SaveChanges();
            }
        }

        public static List<gmlistguests> getByListsGuestsInvites(int evento, int user, int typeInvite)
        {
            //seta a usuário
            List<gmlistguests> lista = null;

            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {
                IQueryable<gmlistguests> query =
                from listGuests in context.gmlistguests
                where listGuests.GMEVENT_CDEVENT == evento && listGuests.FGTYPEINVITE == typeInvite && listGuests.GMUSER_CDUSER == user
                select listGuests;
                lista = query.ToList();
            }
            return lista;
        }

        public static gmlistguests getByListsGuests(int id)
        {
            //seta a usuário
            gmlistguests objListsGuests = null;

            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {
                objListsGuests =
                (from listsguests in context.gmlistguests
                 where listsguests.CDLISTGUESTS == id
                 select listsguests).FirstOrDefault();
            }
            return objListsGuests;
        }

        public static gmlistguests getByListsGuestMail(string mail, int evento)
        {
            //seta a usuário
            gmlistguests objListsGuests = null;

            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {
                objListsGuests =
                (from listsguests in context.gmlistguests
                 where listsguests.GMEVENT_CDEVENT == evento && listsguests.NMMAILGUEST == mail
                 select listsguests).FirstOrDefault();
            }
            return objListsGuests;
        }

        public static List<gmlistguests> getGridListGuests(int cdUser, int cdEvent)
        {
            //seta a variável lista            
            List<gmlistguests> lista = null;

            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {
                IQueryable<gmlistguests> query =
                from listGuests in context.gmlistguests
                where listGuests.GMUSER_CDUSER == cdUser && listGuests.GMEVENT_CDEVENT == cdEvent
                select listGuests;
                lista = query.ToList();
            }
            return lista;
        }

        public static void getList()
        {
            //seta a variável lista            

            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {

                var selectListGuests = from guestlists in context.gmlistguests.Include("gmuser.nmuser")
                                       from gevent in context.gmevent.Include("gmevent.nmevent")
                                       from user in context.gmuser.Include("gmuser.nmuser")
                                       from guests in context.gmguest.Include("gmguests.nmguests")
                                       where guestlists.GMUSER_CDUSER == user.CDUSER && guestlists.GMEVENT_CDEVENT == gevent.CDEVENT
                                       select new { guestlists.CDLISTGUESTS, guests.NMGUEST, guests.NMPHONE, guests.NMMAIL, user.NMUSER, gevent.NMEVENT, gevent.DTEVENT };
                var lista = selectListGuests.ToList();
            }
        }

        public static void deleteListGuest(gmlistguests obj)
        {
            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {
                //verifica se o objeto a ser alterado existe
                gmlistguests gmListGuest = context.gmlistguests.Where(listGuest => listGuest.CDLISTGUESTS == obj.CDLISTGUESTS).FirstOrDefault();

                //caso o objeto exista, apaga o objeto
                if (gmListGuest != null)
                {
                    //apaga o objeto
                    context.DeleteObject(gmListGuest);

                    //salva as alterações
                    context.SaveChanges();
                }
            }
        }
    }
}
